<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Good;
use App\Models\Goods;
use App\Models\GoodsCategory;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use Illuminate\Support\Facades\DB;
use Dcat\Admin\Admin;

class GoodController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Good(), function (Grid $grid) {

            $grid->model()->with('category')->where('country','=',config('app.goods_country'))
                ->orderByDesc('id');

            //$grid->column('id')->sortable();
            $grid->column('code');
            $grid->column('category.title',trans('good.fields.category_title'));
            //$grid->column('type');
            $grid->column('name')->width(300);
            /*
            $grid->column('img')->display(function ($var){
                $src = aliyun_domain() .$var;
                    $html = "<a href='$src' target='_blank'><img src='$src' height='50'> </a>";
                return $html;
            });
            */
            $grid->column('img')->image('',50,50);
            $grid->column('price')->display(function ($var) {
                return $var / 100;
            });
            $grid->column('original_price')->display(function ($var) {
                return $var / 100;
            });
            $grid->column('profit')->display(function ($var) {
                return $var / 100;
            });
            //$grid->column('sales');
            //$grid->column('virtual_sales');
            $grid->column('quality');
            //$grid->column('arrival_day');
            $grid->column('status')->switch();
            //$grid->column('recommend');
            //$grid->column('special_offer');
            //$grid->column('sort');
            //$grid->column('daily_purchase_quantity');
            //$grid->column('created_at');

            $grid->quickSearch('name');

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('category_id')->select($this->getCategory());
                $filter->like('name');
                $filter->equal('status', '商品状态')->select(Goods::STATUS);
                $filter->like('code');
            });

            $grid->paginate(10);
            $grid->showQuickEditButton();
            $grid->enableDialogCreate();
            $grid->disableColumnSelector();
            $grid->disableEditButton();
            $grid->disableViewButton();

        });
    }


    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        Admin::css('/public/static/css/admin.css');
        return Form::make(new Good(), function (Form $form) {
            $form->text('code')->required();
            $form->select('category_id')->options($this->getCategory())->required();
            $form->text('name')->required();

            $form->model()->original_price = $form->model()->original_price / 100;
            $form->text('original_price')->required()->saving(function ($var) {
                $var = $var * 100;
                return $var;
            });
            $form->model()->price = $form->model()->price / 100;
            $form->text('price')->required()->saving(function ($var) {
                $var = $var * 100;
                return $var;
            });

            $form->model()->profit = $form->model()->profit / 100;
            $form->text('profit')->required()->saving(function ($var) {
                $var = $var * 100;
                return $var;
            });
            $form->number('quality')->required();
            //$form->number('arrival_day')->default(3)->min(3)->required();

            $form->image('img')->uniqueName()->required();

            $form->multipleImage('imgs')->uniqueName()->required();

            $form->editor('detail')->required();




            $form->switch('status')->default(1)->required();
            $form->hidden('country')->default(config('app.goods_country'));

        });
    }


    /**
     * 获取商品分类
     * @return \Illuminate\Support\Collection
     */
    protected function getCategory(): \Illuminate\Support\Collection
    {
        return GoodsCategory::query()->where('country','=',config('app.goods_country'))->pluck('title' ,'id');
    }


}
